//
//  JMLoginUserModel.m
//  JMBaseProject
//
//  Created by Liuny on 2020/1/3.
//  Copyright © 2020 liuny. All rights reserved.
//

#import "JMLoginUserModel.h"

@implementation JMLoginUserModel
MJCodingImplementation

-(NSString *)firstInstallTimeStemp{
    if (!_firstInstallTimeStemp) {
        _firstInstallTimeStemp = [NSString stringWithFormat:@"%ld",(NSInteger)[[NSDate new] timeIntervalSince1970] * 1000];;
    }
    return _firstInstallTimeStemp;;
}
-(NSString *)lastStartTimeStemp{
    if (!_lastStartTimeStemp) {
        _lastStartTimeStemp = [NSString stringWithFormat:@"%ld",(NSInteger)[[NSDate new] timeIntervalSince1970] * 1000];;
    }
    return _lastStartTimeStemp;
}
-(BOOL)showFicoWarning{
    NSDate *currentDate = [NSDate new];
    double currentDateInterval = [currentDate timeIntervalSince1970] * 1000;
    double ficoDif = self.ficoExpireTime.doubleValue - currentDateInterval;
    BOOL showFicoWarning = ficoDif <= 7 * 24 * 60 * 60 * 1000;
    return showFicoWarning&&self.ficoExpireTime!=nil;
}
-(NSString *)ficoExpireTimeString{
    if (self.ficoExpireTime.intValue>0) {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.ficoExpireTime.intValue*1000];
        return [date.stringWithISOFormat substringToIndex:10];
    }else{
        return @"";
    }
}
-(NSString *)cloudunExpireTimeString{
    if (self.cloudunExpireTime.intValue>0) {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.cloudunExpireTime.intValue*1000];
        return [date.stringWithISOFormat substringToIndex:10];
    }else{
        return @"";
    }
}
-(BOOL)showCloudunWarning{
    NSDate *currentDate = [NSDate new];
    double currentDateInterval = [currentDate timeIntervalSince1970] * 1000;
    double cloudunDif = self.cloudunExpireTime.doubleValue - currentDateInterval;
    BOOL showCloudunWarning = cloudunDif <= 7 * 24 * 60 * 60 * 1000;
    return showCloudunWarning&&self.cloudunExpireTime!=nil;
}
-(BOOL)showFicoPop{
    NSDate *currentDate = [NSDate new];
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"MM-dd-yyyy";
    
    NSString *saveDateString = [[NSUserDefaults standardUserDefaults] stringForKey: @"FicoInfoError"];
    if (saveDateString == nil) {
        saveDateString = @"";
    }
    NSString *currentDateString = [dateFormatter stringFromDate: currentDate];
    if (![saveDateString isEqualToString: currentDateString] && self.ficoInfoError) {
        [[NSUserDefaults standardUserDefaults] setObject:currentDateString forKey:@"FicoInfoError"];
        return YES;
    }
    return NO;
}
-(NSString *)deviceNo{
    if (!_deviceNo) {
        _deviceNo = [[NSUUID UUID] UUIDString];
    }
    return _deviceNo;
    
}
-(NSString *)randomKey{
    if (!_randomKey) {
        _randomKey = [[NSUUID UUID] UUIDString];
    }
    return _randomKey;
    
}
-(NSString *)adid{
    if (!_adid || _adid.length==0) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSString *adId = [Adjust adid];
            NSString *idfa = [Adjust idfa];
            if (adId&&![adId containsString:@"00000"]){
                _adid = adId;
                [self save];
            }else if(idfa.length&&![idfa containsString:@"00000"]){
                _adid = idfa;
                [self save];
            }else{
                _adid = @"";
            }
        });
    }
    return _adid;
}

- (void)save {
    [NSKeyedArchiver archiveRootObject:self toFile:kLoginUserSavePath];
}

- (void)clear {
    [[NSFileManager defaultManager] removeItemAtPath:kLoginUserSavePath error:nil];
}

@end
